/* 
 * File:   TxtLogger.cpp
 * Author: maxds
 * 
 * Created on 10 août 2011, 18:00
 */

#include "TxtLogger.h"

#define MESSAGE "Le postraitement xml se trouvera dans "

TxtLogger::TxtLogger(string &bin) {
    this->bin=bin;
    openFile();
}

TxtLogger::TxtLogger(const TxtLogger& orig) {
}

TxtLogger::~TxtLogger() {
    fclose(out);
}

void TxtLogger::openFile(){
  sprintf(path,"memoryTraceProfile.%s.txt",this->bin.c_str());
  printf("%s %s\n",MESSAGE,path);
  
  out = fopen(path, "w+");
  assert(out!=NULL);
  
  /*ouverture du document xml*/
  fprintf(out,"#fichier txt regroupant les allocations du programme %s\n",bin.c_str());
}

void TxtLogger::startMalloc(void* addr, size_t size, unsigned index, int memory_total){
    fprintf(out,"%u|m|%d|%p\n",index,size,addr);
}

void TxtLogger::startCalloc(void* addr, size_t nmemb, size_t size, int compteur_calloc, unsigned int memory_allocation){
    fprintf(out,"%u|c|%d|%d|%p\n",compteur_calloc,nmemb,size,addr);
}


void TxtLogger::startRealloc(void* v, void* ret, size_t n, int id, size_t tm){
    fprintf(out,"%u|r|%p|%d|%p\n",id,v,n,ret);
}


void TxtLogger::startFree(void* addr, unsigned nbfree){
    fprintf(out,"%u|f|%p\n",nbfree,addr);
}

void TxtLogger::endElement(const char* type){
    
}

void TxtLogger::finalizeLog(alloc_stats& st){
    fprintf(out,"#memory summary\n");
    fprintf(out,"malloc  count = %li\n",st.compteur_malloc);
    fprintf(out,"calloc  count = %li\n",st.compteur_calloc);
    fprintf(out,"realloc count = %li\n",st.compteur_realloc);
    fprintf(out,"free    count = %li\n",st.compteur_free);
    fprintf(out,"#fin du fichier\n");
}

